﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.UserRegistration.Entity;

namespace Ivy.UserRegistration.DataAccess
{
    public class ProjectMemberMasterDao : IDaoInterface<ProjectMemberMaster, ProjectMemberMasterCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        public bool Insert(ProjectMemberMaster ProjectMemberMasterDataEntity)
        {
            bool IsOperationSuccessful = false;
            if (ProjectMemberMasterDataEntity.EntityState == EntityState.New)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ProjectMemberMaster_Insert";
                CmdObj.CommandType = CommandType.StoredProcedure;

                TranslateProjectMemberMasterFromEntityToDatabaseParameters(CmdObj, ProjectMemberMasterDataEntity);

                DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ProjectMemberMasterDataEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@ProjectMemberKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecord = CmdObj.ExecuteNonQuery();
                if (_NoOfRecord == 1)
                {
                    ProjectMemberMasterDataEntity.ProjectMemberKey = Convert.ToInt32(CmdObj.Parameters["@ProjectMemberKey"].Value);
                    ProjectMemberMasterDataEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                    ProjectMemberMasterDataEntity.Sys_DateOfModification = ProjectMemberMasterDataEntity.Sys_DateOfCreation;
                    ProjectMemberMasterDataEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {

                throw new Exception("For Inserting new ProjectMemberMasterData entity data to database,[EntityState] attribute should be [New]");
            }

            return IsOperationSuccessful;
        }

        public bool Update(ProjectMemberMaster ProjectMemberMasterDataEntity)
        {
            bool IsOperationSuccessful = false;

            if (ProjectMemberMasterDataEntity.EntityState == EntityState.Changed)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ProjectMemberMaster_Update";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ProjectMemberKey", ParameterDirection.Input, DbType.Int32, ProjectMemberMasterDataEntity.ProjectMemberKey);

                TranslateProjectMemberMasterFromEntityToDatabaseParameters(CmdObj, ProjectMemberMasterDataEntity);
                DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, ProjectMemberMasterDataEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecords = CmdObj.ExecuteNonQuery();
                if (_NoOfRecords == 1)
                {
                    ProjectMemberMasterDataEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                    ProjectMemberMasterDataEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }

                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {
                throw new Exception("For Updating existing ProjectMemberMasterData entity data to database, [EntityState] attribute should be [Changed]");
            }

            return IsOperationSuccessful;
        }

        public bool Delete(ProjectMemberMaster DataEntity)
        {
            throw new NotImplementedException();
        }

        public ProjectMemberMaster GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<ProjectMemberMaster> GetAll()
        {
            List<ProjectMemberMaster> lProjectMemberMaster = new List<ProjectMemberMaster>();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ProjectMemberMaster_GetAll";
            CmdObj.CommandType = CommandType.StoredProcedure;

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();

            DbDataReader _DataReader = CmdObj.ExecuteReader();
            if (_DataReader.HasRows)
            {
                while (_DataReader.Read())
                {
                    ProjectMemberMaster _TempSys_ProjectMemberMaster = new ProjectMemberMaster(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateProjectMemberMasterFieldsFromReaderToEntity(_TempSys_ProjectMemberMaster, _DataReader);
                    lProjectMemberMaster.Add(_TempSys_ProjectMemberMaster);
                }
            }
            CmdObj.Dispose();
            DbConn.Close();


            return lProjectMemberMaster;
        }

        public List<ProjectMemberMaster> GetByCriteria(ProjectMemberMasterCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }

        public ProjectMemberMaster GetByProjectMemberKey(int ProjectMemberKey)
        {
            ProjectMemberMaster _SelectedProjectMemberMaster = null; //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ProjectMemberMaster_GetByProjectMemberKey";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ProjectMemberKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ProjectMemberKey));


            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _SelectedProjectMemberMaster = new ProjectMemberMaster(); //IMPORTANT NOTE:- Create instance only when required.
                    TranslateProjectMemberMasterFieldsFromReaderToEntity(_SelectedProjectMemberMaster, _DataReader);
                }
            }
            else
            {
                throw new Exception("No ProjectMemberMaster found");
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _SelectedProjectMemberMaster;
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateProjectMemberMasterFieldsFromReaderToEntity(ProjectMemberMaster pProjectMemberMaster, DbDataReader pDbDataReader)
        {

            pProjectMemberMaster.ProjectMemberKey = Convert.ToInt32(pDbDataReader["ProjectMemberKey"]);
            pProjectMemberMaster.ProjectMemberUserKey = Convert.ToInt32(pDbDataReader["ProjectMemberUserKey"]);
            pProjectMemberMaster.ProjectMemberFirstName = Convert.ToString(pDbDataReader["ProjectMemberFirstName"]);
            pProjectMemberMaster.ProjectMemberLastName = Convert.ToString(pDbDataReader["ProjectMemberLastName"]);
            pProjectMemberMaster.ProjectMemberPrimaryEmailID = Convert.ToString(pDbDataReader["ProjectMemberPrimaryEmailID"]);
            pProjectMemberMaster.ProjectMemberCodePlexID = Convert.ToString(pDbDataReader["ProjectMemberCodePlexID"]);
            pProjectMemberMaster.ProjectMemberSkypeID = Convert.ToString(pDbDataReader["ProjectMemberSkypeID"]);
            pProjectMemberMaster.ProjectMemberMicrosoftID = Convert.ToString(pDbDataReader["ProjectMemberMicrosoftID"]);
            pProjectMemberMaster.ProjectMemberGoogleID = Convert.ToString(pDbDataReader["ProjectMemberGoogleID"]);
            pProjectMemberMaster.ProjectMemberTimeZoneReference = Convert.ToString(pDbDataReader["ProjectMemberTimeZoneReference"]);
            pProjectMemberMaster.ProjectMemberTimeZoneName = Convert.ToString(pDbDataReader["ProjectMemberTimeZoneName"]);
            pProjectMemberMaster.ProjectMemberTimeZoneAbbreviation = Convert.ToString(pDbDataReader["ProjectMemberTimeZoneAbbreviation"]);
            pProjectMemberMaster.ProjectMemberCountryName = Convert.ToString(pDbDataReader["ProjectMemberCountryName"]);

            if (pDbDataReader["ProjectMemberDateOfJoining"] != DBNull.Value)
            {
                pProjectMemberMaster.ProjectMemberDateOfJoining = Convert.ToDateTime(pDbDataReader["ProjectMemberDateOfJoining"]);
            }
            else
            {
                pProjectMemberMaster.ProjectMemberDateOfJoining = null;
            }
            
            pProjectMemberMaster.ProjectMemberIsMemberOfFacebook = Convert.ToBoolean(pDbDataReader["ProjectMemberIsMemberOfFacebook"]);

            DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pProjectMemberMaster, pDbDataReader);

        }
        private void TranslateProjectMemberMasterFromEntityToDatabaseParameters(DbCommand pDbCommand, ProjectMemberMaster pProjectMemberMaster)
        {
            //DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberKey", ParameterDirection.Input, DbType.Int32, pProjectMemberMaster.ProjectMemberKey);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberUserKey", ParameterDirection.Input, DbType.Int32, pProjectMemberMaster.ProjectMemberUserKey);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberFirstName", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberFirstName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberLastName", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberLastName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberPrimaryEmailID", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberPrimaryEmailID);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberCodePlexID", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberCodePlexID);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberSkypeID", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberSkypeID);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberMicrosoftID", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberMicrosoftID);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberGoogleID", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberGoogleID);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberTimeZoneReference", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberTimeZoneReference);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberTimeZoneName", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberTimeZoneName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberTimeZoneAbbreviation", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberTimeZoneAbbreviation);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberCountryName", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberCountryName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberCityName", ParameterDirection.Input, DbType.String, pProjectMemberMaster.ProjectMemberCityName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberDateOfJoining", ParameterDirection.Input, DbType.DateTime, pProjectMemberMaster.ProjectMemberDateOfJoining);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ProjectMemberIsMemberOfFacebook", ParameterDirection.Input, DbType.Boolean, pProjectMemberMaster.ProjectMemberIsMemberOfFacebook);
        }
        #endregion Private Methods
        
    }
}
